﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Spymaster.Entities;

namespace Spymaster.Entities.Components
{
    public class KeyboardComponent
    {
        private PlayerEntity entity;

        private KeyboardState oldState;

        // Key Bindings
        private Keys moveLeft = Keys.A;
        public Keys MoveLeft
        {
            get { return moveLeft; }
            set { moveLeft = value; }
        }

        private Keys moveRight = Keys.D;
        public Keys MoveRight
        {
            get { return moveRight; }
            set { moveRight = value; }
        }

        private Keys moveUp = Keys.W;
        public Keys MoveUp
        {
            get { return moveUp; }
            set { moveUp = value; }
        }

        private Keys moveDown = Keys.S;
        public Keys MoveDown
        {
            get { return moveDown; }
            set { moveDown = value; }
        }

        private Keys medkit = Keys.Q;
        public Keys Medkit
        {
            get { return medkit; }
            set { medkit = value; }
        }

        private Keys reload = Keys.R;
        public Keys Reload
        {
            get { return reload; }
            set { reload = value; }
        }

        public KeyboardComponent(PlayerEntity gameEntity)
        {
            this.entity = gameEntity;
            oldState = new KeyboardState();
        }

        public void Update(GameTime gameTime)
        {
            KeyboardState state = Keyboard.GetState();

            if(state.IsKeyDown(moveLeft))
            {
                entity.Position += 
                    new Vector2(-gameTime.ElapsedGameTime.Milliseconds * 
                        entity.PlayerSpeed / 1000.0f, 0.0f);
            }

            if (state.IsKeyDown(moveRight))
            {
                entity.Position += 
                    new Vector2(gameTime.ElapsedGameTime.Milliseconds *
                        entity.PlayerSpeed / 1000.0f, 0.0f);
            }

            if (state.IsKeyDown(moveUp))
            {
                entity.Position += 
                    new Vector2(0.0f, -gameTime.ElapsedGameTime.Milliseconds * 
                        entity.PlayerSpeed / 1000.0f);
            }

            if (state.IsKeyDown(moveDown))
            {
                entity.Position += 
                    new Vector2(0.0f, gameTime.ElapsedGameTime.Milliseconds *
                        entity.PlayerSpeed / 1000.0f);
            }

            if (oldState.IsKeyUp(medkit) && state.IsKeyDown(medkit))
            {
                // TODO: implement medkit
                Console.WriteLine("Medkit used");
            }

            if (oldState.IsKeyUp(reload) && state.IsKeyDown(reload))
            {
                // TODO: implement reload
                Console.WriteLine("Reload gun");
            }

            oldState = state;
        }
    }
}
